/**
 * 
 */
package ac.svn.accesscontrol.ui.window;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import ac.svn.accesscontrol.manager.SVNTagManager;
import ac.svn.accesscontrol.store.SVNTag;
import ac.svn.accesscontrol.ui.StyledTextPane;
import ac.svn.accesscontrol.ui.model.TagListModel;
import ac.svn.accesscontrol.ui.renderer.SimpleIconRenderer;
import ac.svn.accesscontrol.utils.Utility;

/**
 * @author Enigmanic
 *
 */
public final class TagEditorWindow extends JFrame {
	public TagEditorWindow() {
		
		super("Tag Editor");
		setSize(800,600);
		setLocation();
		
		listModel = new TagListModel();
		getContentPane().setLayout(new BorderLayout(5, 5));
		
		statusBar = new JToolBar();
		getContentPane().add(statusBar, BorderLayout.SOUTH);
		
		JSplitPane splitPane = new JSplitPane();
		splitPane.setDividerSize(3);
		splitPane.setBorder(new EmptyBorder(0, 0, 0, 0));
		getContentPane().add(splitPane, BorderLayout.CENTER);
		
		JScrollPane tagScrollPane = new JScrollPane();
		tagScrollPane.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.RAISED, null, null), "Tags", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		splitPane.setLeftComponent(tagScrollPane);
		
		tagsListView = new JList();
		tagsListView.setBorder(UIManager.getBorder("TextField.border"));
		tagsListView.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		tagsListView.setModel(listModel);
		tagsListView.setCellRenderer(new SimpleIconRenderer("images/tag.png"));
		tagScrollPane.setViewportView(tagsListView);
		
		JPanel tagPropertyPanel = new JPanel();
		tagPropertyPanel.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.RAISED, null, null), "Properties", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		splitPane.setRightComponent(tagPropertyPanel);
		tagPropertyPanel.setLayout(new BorderLayout(0, 0));
		
		JScrollPane scrollPane = new JScrollPane();
		tagPropertyPanel.add(scrollPane, BorderLayout.CENTER);
		
		textPane = new StyledTextPane();
		textPane.setEditable(false);
		scrollPane.setViewportView(textPane);
		splitPane.setDividerLocation(0.35);
		
		JLabel lblIntroLabel = new JLabel("<HTML>\r\n<p align=\"CENTER\"> \r\nWelcome, This is a tag analyser and editor utility. List on the left represent various tag in your access file and section on right state properties of a  tag in System.\r\n</p>\r\n</HTML>");
		lblIntroLabel.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.RAISED, null, null), "Tag Editor", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		getContentPane().add(lblIntroLabel, BorderLayout.NORTH);
		
		menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		mnAction = new JMenu("Action");
		mnAction.setMnemonic('A');
		menuBar.add(mnAction);
		
		JMenuItem miClose = new JMenuItem("Close");
		miClose.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				setLocation();
				setVisible(false);
			}
		});
		miClose.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0));
		
		miGenerateTags = new JMenuItem("Generate tags");
		mnAction.add(miClose);
		mnAction.add(miGenerateTags);
		
		JMenu mnHelp = new JMenu("Help");
		mnHelp.setMnemonic('H');
		menuBar.add(mnHelp);
		
		JMenuItem miNoHelpAvailable = new JMenuItem("No Help Available");
		mnHelp.add(miNoHelpAvailable);
		
		Utility.addEscapeListener(this);
	}

	private void setLocation() {
		Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation((int)(size.getWidth()/2 - getWidth()/2  ),(int)(size.getHeight()/2 - getHeight()/2));
	}

	public void showTagEditor()
	{
		listModel.clear();
		for(String tagId:SVNTagManager.getTagManager().getTagIds())
		{
			SVNTag tag = SVNTagManager.getTagManager().getSvnTag(tagId);
			listModel.addElement(tag);
		}
		
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				SwingUtilities.updateComponentTreeUI(tagsListView);
				setVisible(true);
			}
		});
	}
	
	public StyledTextPane getTextPane() {
		return textPane;
	}

	
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JToolBar statusBar;
	private JMenuBar menuBar;
	private TagListModel listModel;
	private JList tagsListView;
	private StyledTextPane textPane;
	private JMenu mnAction;
	private JMenuItem miGenerateTags;
	public static void main(String args[])
	{
		(new TagEditorWindow()).setVisible(true);
	}

	public JList getTagsListView() {
		return tagsListView;
	}

	public JMenuItem getMIGenerateTags() {
		return miGenerateTags;
	}
}
